<?php 
	require_once('../session.inc');
	if ($_SESSION['IsLogin'] == 0)
	{
		header("Location:index.php");
	}
	//kiểm tra tên gian hàng có tồn tại hay không
	if(isset($_REQUEST['type'])	&& is_numeric($_REQUEST['type']))
	{
		require_once('../class/GianHangDAO.php');
		require_once('../class/DoiTuongDAO.php');
		require_once('../class/NguoiDungDAO.php');
		
		if ($_REQUEST['type'] == 0)
		{
			if(isset($_REQUEST['tenGH']) && isset($_REQUEST['t']))
			{
				//kiểm tra tên gian hàng có tồn tại
				$gianHangDto = GianHangDAO::LayGianHangTheoTenGH($_REQUEST['tenGH'], 0);
				if (is_null($gianHangDto))
				{
					echo '<div style="color:green;" >Tên gian hàng chưa có ai sử dụng</div>
					<a href="#" onclick="funcKiemTraTonTaiTenGianHang()" >Kiểm tra tồn tại</a>';
										
				}
				else
				{
					echo  '<div style="color:red;">Tên gian hàng đã tồn tại</div>
					<a href="#" onclick="funcKiemTraTonTaiTenGianHang()" >Kiểm tra tồn tại</a>';
				}
			}
		}
		//tạo gian hàng
		else if ($_REQUEST['type'] == 1)
		{
			//error
			//1:không tạo folder, 2: ko hỗ trợ loại file, 
			//3: up ảnh bị lỗi, 4: tạo gian hàng lỗi, 5: tạo đối tượng gh lỗi, 6: tạo hành công
			
			//XỬ LÝ FILE
			//kiểm tra xem có folder chưa, chưa thì tạo.
			$structure ="../users/".$_SESSION['UserName']."/product/";
			if (!is_dir($structure))
			{
				if (!mkdir($structure, 0, true)) {
					die('Failed to create folders...');
					return;
				}
			}
			$structure ="../users/".$_SESSION['UserName']."/theme/";
			if (!is_dir($structure))
			{
				if (!mkdir($structure, 0, true)) {
					die('Failed to create folders...');
					return;
				}
			}
			
			$types = array('image/jpeg', 'image/gif','image/pjpeg'); 		
			// kiểm tra file upload		
			if (count($_FILES) > 0)
			{
				//LOGO
				if ($_FILES['fileLogo']['size'] > 0)
				{
					$fileLogo = $_FILES['fileLogo'];
					if (!in_array($fileLogo['type'], $types)) 
					{						
						header("Location: ../tao_gian_hang.php?error=2");
						return;
					}		
					
					if ($fileLogo['error'] != 0)
					{						
						header("Location: ../tao_gian_hang.php?error=3");						
						return;
					}
					// move file & đổi tên ...
					$path_parts = pathinfo($fileLogo['name']);
					$fileTemp = $_SESSION['UserName'].'_logo.'.$path_parts['extension'] ;
					$fileNameLogo = 'users/'.$_SESSION['UserName'].'/theme/'.$fileTemp;					
										
					if (!move_uploaded_file($fileLogo['tmp_name'], $fileTemp))
					{
						header("Location: ../tao_gian_hang.php?error=3");
						return;
					}	
					//move về thư mục đúng
					rename($fileTemp, '../'.$fileNameLogo);
				}
				if ($_FILES['fileBanner']['size'] > 0)
				{
					$fileBanner = $_FILES['fileBanner'];
					
					if (!in_array($fileBanner['type'], $types)) 
					{
						header("Location: ../tao_gian_hang.php?error=2");	
						return;
					}		
					
					if ($fileBanner['error'] != 0)
					{
						
						header("Location: ../tao_gian_hang.php?error=3");
						return;
					}
					// move file & đổi tên ...
					$path_parts = pathinfo($fileBanner['name']);					
					$fileTemp = $_SESSION['UserName'].'_banner.'.$path_parts['extension'] ;
					$fileNameBanner = 'users/'.$_SESSION['UserName'].'/theme/'.$fileTemp;				
					if (!move_uploaded_file($fileBanner['tmp_name'], $fileTemp))
					{
						header("Location: ../tao_gian_hang.php?error=3");
						return;
					}	
					//move
					rename($fileTemp, '../'.$fileNameBanner);
				}
				//upfile thành công
			}	
			
			// XỬ LÝ XML
			$sloganGH = trim($_REQUEST['txtSloganGH']);
			$diaChiGH = trim($_REQUEST['txtDiaChiGH']);
			$gioiThieuGH = trim($_REQUEST['txtGioiThieuGH']);
			$footerGH = trim($_REQUEST['txtFooter']);
						
			$doc = new DOMDocument('1.0','utf-8');
			// we want a nice output
			$doc->formatOutput = true;
			
			$root = $doc->createElement('ThongTin');
			$root = $doc->appendChild($root);
			
			$element = $doc->createElement('slogan');
			$element = $root->appendChild($element);			
			$text = $doc->createTextNode($sloganGH);
			$text = $element->appendChild($text);
			
			$element = $doc->createElement('diachi');
			$element = $root->appendChild($element);			
			$text = $doc->createTextNode($diaChiGH);
			$text = $element->appendChild($text);
			
			$element = $doc->createElement('gioithieu');
			$element = $root->appendChild($element);			
			$text = $doc->createTextNode($gioiThieuGH);
			$text = $element->appendChild($text);
			
			$element = $doc->createElement('footer');
			$element = $root->appendChild($element);			
			$text = $doc->createTextNode($footerGH);
			$text = $element->appendChild($text);
			
			$element = $doc->createElement('logo');
			$element = $root->appendChild($element);			
			$text = $doc->createTextNode($fileNameLogo);
			$text = $element->appendChild($text);
			
			$element = $doc->createElement('banner');
			$element = $root->appendChild($element);			
			$text = $doc->createTextNode($fileNameBanner);
			$text = $element->appendChild($text);
			
			
			$xml = $doc->saveXML();
			$doc->save('info.xml');
			//move file
			if (file_exists('info.xml'))
			{
				$path = "../users/".$_SESSION['UserName']."/theme/";
				//echo $path;
				if (!is_dir($path))
				{
					if (!mkdir($path, 0, true)) 
					{
						header("Location: ../tao_gian_hang.php?error=1");
						return;
					}
				}
				rename('info.xml', $path.'info.xml');
			}			
			
					
			
			//tạo đối tượng
			$res = DoiTuongDAO::ThemDoiTuong('Su Kien');
			if ($res)
			{
				$gianHangDto = new GianHangDTO();
				$gianHangDto->MaGianHang = $res;	
				$gianHangDto->TenGianHang = $_REQUEST['txtTenGH'];			
				$gianHangDto->MaNguoiDung = $_SESSION['IdUser'];
				$gianHangDto->NgayTao = date('Y-m-d h:m:s');
				$gianHangDto->DaXoa = 0;
				$gianHangDto->LuotXem = 0;
				$gianHangDto->ThongTin = 'users/'.$_SESSION['UserName'].'/theme/info.xml';
				$gianHangDto->Theme = $_REQUEST['radTheme'];
				
				if (GianHangDAO::ThemGianHang($gianHangDto))
				{
					//update mã gian hàng của người dùng
					if (NguoiDungDAO::CapNhatMaGHCuaNguoiDung($_SESSION['IdUser'], $gianHangDto->MaGianHang))
					{						
						$_SESSION['Authentication'] = 2;
						header("Location: ../tao_gian_hang.php?error=6&maGH=$res");
					}
				}
				else
				{
					header("Location: ../tao_gian_hang.php?error=4");
				}
			}	
			else
			{
				header("Location: ../tao_gian_hang.php?error=5");
			}
		}
		else
		{
			//Cập nhật 
			if ($_REQUEST['type'] == 2 && isset($_REQUEST['hidMaGH']))
			{
				$maGH = $_REQUEST['hidMaGH'];
				$gianHang_Old = GianHangDAO::LayGianHangTheoMa($maGH, 0);
				//error
				//1:không tạo folder, 2: ko hỗ trợ loại file, 
				//3: up ảnh bị lỗi, 4: tạo gian hàng lỗi, 5: tạo đối tượng gh lỗi, 6: tạo hành công
				
				//XỬ LÝ FILE
				//kiểm tra xem có folder chưa, chưa thì tạo.
				$structure ="../users/".$_SESSION['UserName']."/product/";
				if (!is_dir($structure))
				{
					if (!mkdir($structure, 0, true)) {
						die('Failed to create folders...');
						return;
					}
				}
				$structure ="../users/".$_SESSION['UserName']."/theme/";
				if (!is_dir($structure))
				{
					if (!mkdir($structure, 0, true)) {
						die('Failed to create folders...');
						return;
					}
				}
				
				$types = array('image/jpeg', 'image/gif','image/pjpeg'); 	
				$fileNameLogo  = "";	
				$fileNameBanner = "";
				// kiểm tra file upload		
				if (count($_FILES) > 0)
				{
					//LOGO
					if ($_FILES['fileLogo']['size'] > 0)
					{
						$fileLogo = $_FILES['fileLogo'];
						if (!in_array($fileLogo['type'], $types)) 
						{						
							header("Location: ../cap_nhat_theme.php?error=2&maGianHang=$maGH");
							return;
						}		
						
						if ($fileLogo['error'] != 0)
						{						
							header("Location: ../cap_nhat_theme.php?error=3&maGianHang=$maGH");						
							return;
						}
						// move file & đổi tên ...
						$path_parts = pathinfo($fileLogo['name']);
						$fileTemp = $_SESSION['UserName'].'_logo.'.$path_parts['extension'] ;
						$fileNameLogo = 'users/'.$_SESSION['UserName'].'/theme/'.$fileTemp;					
											
						if (!move_uploaded_file($fileLogo['tmp_name'], $fileTemp))
						{
							header("Location: ../cap_nhat_theme.php?error=3&maGianHang=$maGH");
							return;
						}	
						//move về thư mục đúng
						rename($fileTemp, '../'.$fileNameLogo);
					}
					else
						$fileNameLogo  =  $gianHang_Old->Logo;
						
					if ($_FILES['fileBanner']['size'] > 0)
					{
						$fileBanner = $_FILES['fileBanner'];
						
						if (!in_array($fileBanner['type'], $types)) 
						{
							header("Location: ../cap_nhat_theme.php?error=2&maGianHang=$maGH");	
							return;
						}		
						
						if ($fileBanner['error'] != 0)
						{
							
							header("Location: ../cap_nhat_theme.php?error=3&maGianHang=$maGH");
							return;
						}
						// move file & đổi tên ...
						$path_parts = pathinfo($fileBanner['name']);					
						$fileTemp = $_SESSION['UserName'].'_banner.'.$path_parts['extension'] ;
						$fileNameBanner = 'users/'.$_SESSION['UserName'].'/theme/'.$fileTemp;				
						if (!move_uploaded_file($fileBanner['tmp_name'], $fileTemp))
						{
							header("Location: ../cap_nhat_theme.php?error=3&maGianHang=$maGH");
							return;
						}	
						//move
						rename($fileTemp, '../'.$fileNameBanner);
					}
					else
						$fileNameBanner = $gianHang_Old->Banner;
					//upfile thành công
				}	
				else
				{
					$fileNameLogo = $gianHang_Old->Logo;
					$fileNameBanner = $gianHang_Old->Banner;
				}
				// XỬ LÝ XML
				$sloganGH = trim($_REQUEST['txtSloganGH']);
				$diaChiGH = trim($_REQUEST['txtDiaChiGH']);
				$gioiThieuGH = trim($_REQUEST['txtGioiThieuGH']);
				$footerGH = trim($_REQUEST['txtFooter']);
							
				$doc = new DOMDocument('1.0','utf-8');
				// we want a nice output
				$doc->formatOutput = true;
				
				$root = $doc->createElement('ThongTin');
				$root = $doc->appendChild($root);
				
				$element = $doc->createElement('slogan');
				$element = $root->appendChild($element);			
				$text = $doc->createTextNode($sloganGH);
				$text = $element->appendChild($text);
				
				$element = $doc->createElement('diachi');
				$element = $root->appendChild($element);			
				$text = $doc->createTextNode($diaChiGH);
				$text = $element->appendChild($text);
				
				$element = $doc->createElement('gioithieu');
				$element = $root->appendChild($element);			
				$text = $doc->createTextNode($gioiThieuGH);
				$text = $element->appendChild($text);
				
				$element = $doc->createElement('footer');
				$element = $root->appendChild($element);			
				$text = $doc->createTextNode($footerGH);
				$text = $element->appendChild($text);
				
				$element = $doc->createElement('logo');
				$element = $root->appendChild($element);			
				$text = $doc->createTextNode($fileNameLogo);
				$text = $element->appendChild($text);
				
				$element = $doc->createElement('banner');
				$element = $root->appendChild($element);			
				$text = $doc->createTextNode($fileNameBanner);
				$text = $element->appendChild($text);
				
				
				$xml = $doc->saveXML();
				$doc->save('info.xml');
				//move file
				if (file_exists('info.xml'))
				{
					$path = "../users/".$_SESSION['UserName']."/theme/";
					//echo $path;
					if (!is_dir($path))
					{
						if (!mkdir($path, 0, true)) 
						{
							header("Location: ../cap_nhat_theme.php?error=1&maGianHang=$maGH");
							return;
						}
					}
					rename('info.xml', $path.'info.xml');
				}			
								
				$ThongTin = 'users/'.$_SESSION['UserName'].'/theme/info.xml';
				$Theme = $_REQUEST['radTheme'];
				
				if (GianHangDAO::CapNhatGianHang($ThongTin, $Theme, $_REQUEST['hidMaGH']))
				{
					
					header("Location: ../cap_nhat_theme.php?error=5&maGianHang=$maGH");
				}
				else
				{
					return;
					header("Location: ../cap_nhat_theme.php?error=4&maGianHang=$maGH");
				}
			
			}
		}
	}
	
?>